package com.jwgf.report;

import java.util.ArrayList;
import java.util.HashSet;
//一天的信息
public class DayInfo {
	private ShiftInfo[] shiftInfoArray = new ShiftInfo[4];

	// index > 0 && index < 4
	public void setShiftInfo(int index, ShiftInfo info) {
		shiftInfoArray[index] = info;
	}

	public ShiftInfo getShiftInfo(int index) {
		return shiftInfoArray[index];
	}

	public ArrayList<ShiftInfo> getValidShiftInfo() {
		ArrayList<ShiftInfo> infoList = new ArrayList<ShiftInfo>();
		for (ShiftInfo info : shiftInfoArray) {
			if (info.getNum() != 0) {
				infoList.add(info);
			}
		}
		return infoList;
	}
	
	public ArrayList<ShiftInfo> getAllShiftInfo() {
		ArrayList<ShiftInfo> infoList = new ArrayList<ShiftInfo>();
		for (ShiftInfo info : shiftInfoArray) {
			infoList.add(info);
		}
		return infoList;
	}
	
	public boolean checkValid() {
		// One day at least 1 shift and at most 4 shifts
		if (getCnt() > 4 || getCnt() < 1) {
			return false;
		}
		// Shift time can not be duplicated
		if (duplicateRemovalCnt() != getCnt()) {
			return false;
		}
		// one day should be 24 hours
		if (is24Hours()) {
			return true;
		} else {
			return false;
		}

	}

	private int getCnt() {
		return getValidShiftInfo().size();
	}

	private int duplicateRemovalCnt() {
		HashSet<ShiftInfo> set = new HashSet<ShiftInfo>();
		for (ShiftInfo info : getValidShiftInfo()) {
			set.add(info);
		}
		return set.size();
	}

	private boolean is24Hours() {
		ArrayList<ShiftInfo> infoList = new ArrayList<ShiftInfo>();
		for (ShiftInfo info : shiftInfoArray) {
			if (info.getNum() != 0) {
				infoList.add(info);
			}
		}
		infoList.add(infoList.get(0));
		int totalMinutes = 0;
		for (int i = 1; i < infoList.size(); ++i) {
			int start = infoList.get(i - 1).getTotalMinutes();
			int end = infoList.get(i).getTotalMinutes();
			if (start >= end) {
				end += 24 * 60;
			}
			totalMinutes += end - start;
		}

		if (totalMinutes == 24 * 60) {
			return true;
		} else {
			return false;
		}
	}
}
